<template>
	<view class="content">
		<!-- 轮播图 -->
		<swiper indicator-dots indicator-color="rgba(255,255,255,.3)" indicator-active-color="#fff" autoplay circular>
			<swiper-item v-for="item in swiperList">
				<image :src="item.img"></image>
			</swiper-item>
		</swiper>
		<!-- 导航部分 -->
		<view class="nav">
			<view class="navItem" v-for="(item, index) in navList" :key="index" @click="navitemClick(item.path)">
				<view :class="item.icon"></view>
				<text>{{item.text}}</text>
			</view>
		</view>
		<!-- 商品列表部分 -->
		<view class="hotGoods">
			<view class="hotTitle">
				<text>推荐商品</text>
			</view>
			<!-- 商品列表组件 -->
			<GoodsList :hotGoods='hotGoods' @itemClick="goDetail"></GoodsList>
		</view>
	</view>
</template>

<script>
	import GoodsList from "../../components/goodsList/goodsList.vue"
	export default {
		components: {
			GoodsList
		},
		data() {
			return {
				swiperList: [],
				navList: [{
						text: '黑马商城',
						icon: 'iconfont icon-ziyuan',
						path: '/pages/goods/goods'
					},
					{
						text: '联系我们',
						icon: 'iconfont icon-guanyuwomen',
						path: '/pages/contact/contact'
					},
					{
						text: '社区图片',
						icon: 'iconfont icon-tupian',
						path: '/pages/pic/pic'
					},
					{
						text: '学习视频',
						icon: 'iconfont icon-shipin',
						path: '/pages/viedo/viedo'
					}
				],
				hotGoods: []
			}
		},
		onLoad() {
			this.getLunbo()
			this.getHotGoods()
		},
		methods: {
			// promise是一个异步函数需要用aysnc
			async getLunbo() {
				// 创建一个变量用来接收函数的返回值
				const res = await this.$myRequest({ //给函数传递参数，参数是一个对象{} url是地址 
					url: "/api/getlunbo"
				})
				// console.log(res);
				this.swiperList = res.data.message
			},
			async getHotGoods() {
				const res = await this.$myRequest({
					url: '/api/getgoods?pageindex=1'
				})
				// console.log(res)
				this.hotGoods = res.data.message
			},
			navitemClick(url) {
				uni.navigateTo({
					url
				})

			},
			goDetail(id) {
				uni.navigateTo({
					url: '/pages/goodDetail/goodDetail?id=' + id
				})
			}
		}
	}
</script>

<style lang="scss">
	.content {
		swiper {
			width: 750rpx;
			height: 380rpx;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.nav {
			display: flex;

			.navItem {
				width: 25%;
				text-align: center;
				font-size: 30rpx;

				view {
					width: 120rpx;
					height: 120rpx;
					background-color: #b50e03;
					margin: 8px auto;
					border-radius: 50%;
					line-height: 120rpx;
					color: #fff;
					font-size: 25px;
				}

				.icon-tupian {
					font-size: 22px;
				}
			}

		}

		.hotGoods {
			margin-top: 10px;
			background-color: #eee;
			overflow: hidden;

			.hotTitle {
				color: $shop-color;
				background-color: #fff;
				margin-top: 10rpx;
				font-size: 40rpx;
				height: 100rpx;
				text-align: center;
				line-height: 100rpx;
				letter-spacing: 20px;
			}


		}
	}
</style>
